package com.service.business.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.business.pay.controller.rep.PayTypeRep;
import com.service.business.pay.service.PayService;
import com.service.business.pay.service.PayTypeService;
import com.service.business.product.controller.rep.ProductPayTypeRep;
import com.service.business.product.service.ProductPayTypeService;
import com.service.core.exception.CommonException;
import com.service.dao.enums.CommonStatusEnums;
import com.service.dao.mapper.ProductPayTypeMapper;
import com.service.dao.model.PayType;
import com.service.dao.model.ProductPayType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Slf4j
@Service("productPayTypeService")
public class ProductPayTypeServiceImpl extends ServiceImpl<ProductPayTypeMapper, ProductPayType> implements ProductPayTypeService {

    @Autowired
    private PayService payService;
    @Autowired
    private PayTypeService payTypeService;

    @Override
    public List<ProductPayTypeRep> listByOrderType(Integer orderType) {

        List<ProductPayType> list = list(new QueryWrapper<ProductPayType>().lambda()
                .eq(ProductPayType::getProductType, orderType)
                .eq(ProductPayType::getProductPayStatus, CommonStatusEnums.ACTIVE.getCode()));

        if (ObjectUtils.isEmpty(list)) {
            throw new CommonException("该产品未配置支付方式", "product_pay_type_not_exist");
        }
        return list.stream().map(this::apply).filter(item -> !ObjectUtils.isEmpty(item)).collect(Collectors.toList());

    }

    @Override
    public List<PayTypeRep> listByOrderTypeAndPayGroup(Integer orderType, Integer payGroup) {
        ProductPayType productPayType = getOne(new QueryWrapper<ProductPayType>().lambda()
                .eq(ProductPayType::getProductType, orderType)
                .eq(ProductPayType::getPayGroup, payGroup)
                .eq(ProductPayType::getProductPayStatus, CommonStatusEnums.ACTIVE.getCode()).last(" limit 1"));

        if (ObjectUtils.isEmpty(productPayType)) {
            log.error("产品<orderType:{}>获取支付方式:{}失败:此产品未配置此支付方式", orderType, payGroup);
            throw new CommonException("该产品未配置支付方式", "product_pay_type_not_exist");
        }

        return payTypeService.listByPayGroup(payGroup);
    }

    @Override
    public Map<String, PayTypeRep> list2MapByOrderTypeAndPayGroup(Integer orderType, Integer payGroup) {
        List<PayTypeRep> list = listByOrderTypeAndPayGroup(orderType, payGroup);
        return list.stream().collect(Collectors.toMap(PayTypeRep::getCoinsName, a -> a));
    }

    private ProductPayTypeRep apply(ProductPayType item) {
        List<PayTypeRep> repList = payTypeService.listByPayGroup(item.getPayGroup());
        if (ObjectUtils.isEmpty(repList)) {
            return null;
        }
        ProductPayTypeRep payTypeRep = new ProductPayTypeRep();
        payTypeRep.setPayTypeList(repList);
        payTypeRep.setPayName(repList.get(0).getPayName());
        payTypeRep.setPayGroup(item.getPayGroup());
        return payTypeRep;
    }

    @Override
    public List<ProductPayTypeRep> listValidPayTypeByOrderType(BigDecimal totalMoney, Integer memberId, Integer orderType) {
        return listByOrderType(orderType).stream()
                .filter(item -> payService.isEnoughPayOrder(totalMoney, memberId, item.getPayTypeList()))
                .collect(Collectors.toList());
    }
}
